package com.taxshare.seller.util;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class LqHttpUtil {

    @Value("${lqpt.ptbh}")
    private String ptbh;
    @Value("${lqpt.hexKey}")
    private String hexKey;
    @Value("${lqpt.url}")
    private String url;
    @Value("${lqpt.ip}")
    private String ip;
    @Value("${lqpt.macdz}")
    private String macdz;
    @Value("${empower.url}")
    private String empowerUrl;

    /**
     * 获取headers
     * @param ptbh
     * @return
     */
    public JSONObject getHeaders(String ptbh) {
        JSONObject headers = new JSONObject();
        headers.put("jrdwptbh",ptbh);//直连单位乐企 ID
        headers.put("sydwptbh",ptbh);//使用单位乐企 ID
        headers.put("ylbm","911001");//用例编码
        headers.put("sxcsbz","");//沙箱测试标志
        headers.put("access_signature","");//签名加密串
        log.info("headers=======>"+headers);
        return headers;
    }

    /**
     * 调用服务。期服务为定向转发乐企接口
     * @param reqStr
     * @param url
     * @return
     * @throws Exception
     */
    public JSONObject sendPostLq(String reqStr, String url) throws Exception {
        JSONObject resultJson = new JSONObject();
        JSONObject headers = getHeaders(ptbh);
        String data = SM4Util.encryptEcb(hexKey,reqStr);
        String timestamp = DateUtils.getDateToStrHms();
        String appKey = "7b43bb2b4ad34375a9fc4bcdf39b835e";
        String appSecret = "ndE2jdZNFixH9G6Aidsfyf7lYT3PxW";
        String signMy = "appKey=" + appKey + "&timestamp=" + timestamp + "&" + data;
        String sign = SM4Util.sha256_HMAC(signMy, appSecret);
        log.info("========sign=====>"+sign);
        JSONObject req_json = new JSONObject();
        req_json.put("timestamp",timestamp);
        req_json.put("sign",sign);
        req_json.put("data",data);
        req_json.put("headers",headers);
        req_json.put("url",url);
        String resStr = HttpUtil.sendPost(empowerUrl+"/v1/api/asyncLogin/postLq",req_json.toString());
        JSONObject resJson = JSONObject.parseObject(resStr);
        log.info("========出参======>>>"+resStr);
        String res_code = resJson.getString("code");
        String res_msg = resJson.getString("msg");
        if("0000".equals(res_code)) {
            String dataStr = resJson.getString("data");
            JSONObject dataJson = JSONObject.parseObject(dataStr);
            String httpStatusCode = dataJson.getString("httpStatusCode");
            if ("200".equals(httpStatusCode)) {
                String body_str = dataJson.getString("body");
                JSONObject body_json = JSONObject.parseObject(body_str);
                String response_str = body_json.getString("Response");
                JSONObject response_json = JSONObject.parseObject(response_str);
                String data_str = response_json.getString("Data");
                if (StringUtils.isNotEmpty(data_str)) {
                    String result = SM4Util.decryptEcb(hexKey, data_str);
                    log.info("========出参解密后======>>>" + result);
                    resultJson = JSONObject.parseObject(result);
                }else {
                    String error_str = response_json.getString("Error");
                    resultJson.put("returncode", "0002");
                    resultJson.put("msg", error_str);
                }
            }else{
                resultJson.put("returncode","0001");
                resultJson.put("msg","接口异常，httpStatusCode："+httpStatusCode);
            }
        }else{
            resultJson.put("returncode","0001");
            resultJson.put("msg",res_msg);
        }
        return resultJson;
    }
}
